package com.airlines.crewassignment.service.basicinfo.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.airlines.base.constant.BaseConstant;
import com.airlines.common.Page;
import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.dao.basicinfo.FoundationMapper;
import com.airlines.crewassignment.entity.basicinfo.Foundation;
import com.airlines.crewassignment.service.basicinfo.FoundationService;

/**
 * @author xxxx
 * @date 2018/06/06 16:10:35
 * @company xxxx Airlines
 * @description 基地服务
 */
@Service
public class FoundationServiceImpl implements FoundationService {

    @Autowired
    private FoundationMapper foundationMapper;

    @Override
    public int insert(Foundation foundation){
        return foundationMapper.insert(foundation);
    }

    @Override
    public int deleteLogic(String base){
        return foundationMapper.deleteLogic(base);
    }


    @Override
    public int update(Foundation foundation){
        return foundationMapper.update(foundation);
    }

    @Override
    public Foundation get(String base){
        return foundationMapper.get(base);
    }

    @Override
    public List<Foundation> list(Foundation foundation){
        return foundationMapper.list(foundation);
    }

    @Override
    public Page list(Foundation foundation, Page page){
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<Foundation> list = foundationMapper.listByFuzzy(foundation);
        PageInfo<Foundation> pageInfo = new PageInfo<Foundation>(list);
        page.setTotal(String.valueOf(pageInfo.getTotal()));
        page.setRows(pageInfo.getList());
        return page;
    }

	@Override
	public boolean isBase(String airport, String dutyCode) {
		Foundation base = this.get(airport);
		return isBase(base, dutyCode);
	}

	private  boolean isBase(Foundation base,String dutyCode){
		if(base == null || base.getIsDelete() == 1){
			return false;
		}
		
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_F) && StringUtils.equals(base.getIsCrew(), BaseConstant.Y)){
			return true;
		}

		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_C) && StringUtils.equals(base.getIsStew(), BaseConstant.Y)){
			return true;
		}
		
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_A) && StringUtils.equals(base.getIsPolice(), BaseConstant.Y)){
			return true;
		}
		
		return false;
	}
	@Override
	public List<Foundation> list(String dutyCode) {
		List<Foundation> listAll = foundationMapper.list(new Foundation());
		List<Foundation> list = new ArrayList<>();
		for(Foundation f : listAll){
			if(isBase(f, dutyCode)){
				list.add(f);
			}
		}
		
		return list;
	}




}
